import 'dart:math';
import '../models/notification.dart' as app_notification;

class NotificationService {
  // 模拟获取用户通知列表
  Future<List<app_notification.Notification>> getNotifications(String userId, {int limit = 20}) async {
    // 模拟API延迟
    await Future.delayed(Duration(milliseconds: 800));
    
    // 模拟通知数据
    final List<app_notification.Notification> notifications = [];
    final random = Random();
    
    // 生成点赞通知
    notifications.add(
      app_notification.Notification(
        id: '1',
        userId: 'user_${random.nextInt(1000)}',
        targetUserId: userId,
        content: '小花喜欢了你的帖子',
        imageUrl: 'https://randomuser.me/api/portraits/women/${random.nextInt(100)}.jpg',
        relatedId: 'post_1',
        relatedType: 'post',
        type: app_notification.NotificationType.like,
        createdAt: DateTime.now().subtract(Duration(minutes: random.nextInt(60))),
      ),
    );
    
    // 生成评论通知
    notifications.add(
      app_notification.Notification(
        id: '2',
        userId: 'user_${random.nextInt(1000)}',
        targetUserId: userId,
        content: '阿黄评论了你的帖子: 好可爱的猫咪！',
        imageUrl: 'https://randomuser.me/api/portraits/men/${random.nextInt(100)}.jpg',
        relatedId: 'post_2',
        relatedType: 'post',
        type: app_notification.NotificationType.comment,
        createdAt: DateTime.now().subtract(Duration(hours: random.nextInt(24))),
      ),
    );
    
    // 生成关注通知
    notifications.add(
      app_notification.Notification(
        id: '3',
        userId: 'user_${random.nextInt(1000)}',
        targetUserId: userId,
        content: '悠悠关注了你',
        imageUrl: 'https://randomuser.me/api/portraits/women/${random.nextInt(100)}.jpg',
        relatedId: 'user_123',
        relatedType: 'user',
        type: app_notification.NotificationType.follow,
        createdAt: DateTime.now().subtract(Duration(days: random.nextInt(7))),
      ),
    );
    
    // 生成系统通知
    notifications.add(
      app_notification.Notification(
        id: '4',
        userId: 'system',
        targetUserId: userId,
        content: '欢迎使用宠物乐园App！点击查看新手指南',
        relatedId: 'guide_1',
        relatedType: 'guide',
        type: app_notification.NotificationType.system,
        createdAt: DateTime.now().subtract(Duration(days: 1)),
      ),
    );
    
    // 生成私信通知
    notifications.add(
      app_notification.Notification(
        id: '5',
        userId: 'user_${random.nextInt(1000)}',
        targetUserId: userId,
        content: '你好，我想咨询一下你发布的那只猫咪还在领养吗？',
        imageUrl: 'https://randomuser.me/api/portraits/men/${random.nextInt(100)}.jpg',
        relatedId: 'chat_1',
        relatedType: 'message',
        type: app_notification.NotificationType.message,
        createdAt: DateTime.now().subtract(Duration(hours: random.nextInt(12))),
      ),
    );
    
    // 按时间排序（最新的在前面）
    notifications.sort((a, b) => b.createdAt.compareTo(a.createdAt));
    
    return notifications;
  }
  
  // 模拟标记通知为已读
  Future<bool> markAsRead(String notificationId) async {
    // 模拟API延迟
    await Future.delayed(Duration(milliseconds: 500));
    
    // 模拟成功响应
    return true;
  }
  
  // 模拟标记所有通知为已读
  Future<bool> markAllAsRead(String userId) async {
    // 模拟API延迟
    await Future.delayed(Duration(milliseconds: 800));
    
    // 模拟成功响应
    return true;
  }
  
  // 模拟删除通知
  Future<bool> deleteNotification(String notificationId) async {
    // 模拟API延迟
    await Future.delayed(Duration(milliseconds: 500));
    
    // 模拟成功响应
    return true;
  }
  
  // 获取未读通知数量
  Future<int> getUnreadCount(String userId) async {
    // 模拟API延迟
    await Future.delayed(Duration(milliseconds: 300));
    
    // 模拟未读数量
    return Random().nextInt(10);
  }
} 